﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HK2009.Common;
using HK2009.BusinessFacade;
using HK2009.BusinessFacadeHandler;
using HK2009.EntityQuery;
using System.Data.SqlClient;
using System.Text;
using System.Collections.Generic;
using System.Threading;
using Microsoft.Reporting.WebForms;

public partial class Reports_VendorBookingReport3 : System.Web.UI.Page
{
    IRolePurviewHandler userManage = new RolePurviewHandler();
    ISyClientHandler clientManage = new SyClientHandler();
    ISyMarketHandler markManage = new SyMarketHandler();
    ISyPlaceHandler placeManage = new SyPlaceHandler();
    ISyDestinationHandler destManage = new SyDestinationHandler();
    IBNBookingNoteHandler bookingNoteManage = new BNBookingNoteHandler();
    DataBase db = new DataBase();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FillDll();
        }

    }
    private void FillDll()
    {

        //绑定托运人
        DataTable dt = clientManage.GetClientListByTypeID(int.Parse(Enums.CT.TYR.ToString("d")));
        ddlShipper.DataSource = dt;

        if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("en"))
        {
            ddlShipper.DataTextField = "ENGShortName";
        }
        else if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("zh-hk"))
        {
            ddlShipper.DataTextField = "HKShortName";
        }
        else
        {
            ddlShipper.DataTextField = "CNShortName";
        }
        ddlShipper.DataValueField = "PKID";
        ddlShipper.DataBind();
        ddlShipper.Items.Insert(0, new ListItem(Resources.Messages.PleaseSelect, "0"));

        //仓库
        dt = clientManage.GetClientListByTypeID(int.Parse(Enums.CT.CK.ToString("d")));
        ddlWhouse.DataSource = dt;
        if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("en"))
        {
            ddlWhouse.DataTextField = "ENGShortName";
        }
        else if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("zh-hk"))
        {
            ddlWhouse.DataTextField = "HKShortName";
        }
        else
        {
            ddlWhouse.DataTextField = "CNShortName";
        }
        ddlWhouse.DataValueField = "PKID";
        ddlWhouse.DataBind();
        ddlWhouse.Items.Insert(0, new ListItem(Resources.Messages.PleaseSelect, "0"));

        //工厂
        dt = clientManage.GetClientListByTypeID(int.Parse(Enums.CT.GC.ToString("d")));
        ddlVendor.Items.Add(new ListItem(Resources.Messages.PleaseSelect, "0"));
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem item = new ListItem();
            if (Thread.CurrentThread.CurrentUICulture.ToString().ToLower().Equals("zh-hk"))
            {
                item.Text = dt.Rows[i]["ENGShortName"].ToString() + "/" + dt.Rows[i]["HKShortName"].ToString();
            }
            else
            {
                item.Text = dt.Rows[i]["ENGShortName"].ToString() + "/" + dt.Rows[i]["CNShortName"].ToString();

            }
            item.Value = dt.Rows[i]["PKID"].ToString();
            ddlVendor.Items.Add(item);
        }
    }

    //有交货日期无收货日期
    #region GetDataTable 获取主报表的数据源
    private DataTable GetDataTable()
    {
        string sqlStr = @"SELECT linkMan.LinkMan, shipper.CNShortName as CNShipper, shipper.HKShortName as HKShipper, shipper.ENGShortName as ENGShipper,
	dtl.ItemNO, vendor.CNShortName as CNVendor, vendor.HKShortName as HKVendor, vendor.ENGShortName as ENGVendor,
	dtl.SONO, whse.CNShortName as CNWhse, whse.HKShortName as HKWhse, whse.ENGShortName as ENGWhse,
	dtl.FCLLCL, dtl.CTN, dtl.VOL, bn.WinStart, bn.WinClose, bn.WinLastClose, 
	customer.CNShortName as CNCustomer, customer.HKShortName as HKCustomer, customer.ENGShortName as ENGCustomer,
	dtl.Remark
FROM BN_BookingNote bn INNER JOIN
	BN_BookingNoteDtl dtl ON bn.PKID = dtl.BookingNoteID LEFT JOIN
	Sy_ClientLinkMan linkMan ON bn.ShipperLMID = linkMan.PKID LEFT JOIN
	Sy_Client shipper ON bn.ShipperID = shipper.PKID LEFT JOIN
	Sy_Client vendor ON dtl.VendorID = vendor.PKID LEFT JOIN
	Sy_Client whse ON dtl.WhouseID = whse.PKID LEFT JOIN
	Sy_Client customer ON bn.CustomerID = customer.PKID
WHERE bn.Status = 1
	AND (dtl.Status = 0 or dtl.Status = 7)
	AND dtl.JobDate is null and dtl.DeliveryDate is not null ";

        SqlParameter[] parms = null;
        string clause = string.Empty;
        ProcessParms(ref clause, ref parms);
        sqlStr += clause;

        return db.RunSqlTab(sqlStr, parms);
    }
    #endregion

    #region Preview Report
    private void PreviewReport()
    {
        rptViewer.LocalReport.ReportPath = "Reports/rptVendorBookingDeliveryDate.rdlc";

        rptViewer.LocalReport.DataSources.Clear();
        DataTable dt = GetDataTable();
        if (dt != null && dt.Rows.Count > 0)
        {
            rptViewer.Visible = true;
            lblNoRecord.Visible = false;
            ReportDataSource datasource = new ReportDataSource("VendorBookingDeliveryDate_VendorBookingDeliveryDate", dt);
            rptViewer.LocalReport.DataSources.Add(datasource);

            ReportParameter[] parameters = new ReportParameter[3];
            parameters[0] = new ReportParameter("DateFrom", txtDateFrom.Text);
            parameters[1] = new ReportParameter("DateTo", txtDateTo.Text);
            parameters[2] = new ReportParameter("DateTimeString", ddlDateString.SelectedItem.Text);
            rptViewer.LocalReport.SetParameters(parameters);
            rptViewer.LocalReport.Refresh();
        }
        else
        {
            rptViewer.Visible = false;
            lblNoRecord.Visible = true;
        }
    }
    #endregion



    /// <summary>
    /// 处理传入参数。。返回SQL条件和sqlparameter
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <param name="parms"></param>
    /// <summary>
    /// 处理传入参数。。返回SQL条件和sqlparameter
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <param name="parms"></param>
    private void ProcessParms(ref string sqlstr, ref SqlParameter[] parms)
    {
        List<SqlParameter> list = new List<SqlParameter>();
        sqlstr = "";

        if (ViewState["Value2"] != null && ViewState["Value3"] != null)
        {
            list.Add(new SqlParameter("@DateFrom", ViewState["Value2"].ToString().ToString()));
            list.Add(new SqlParameter("@DateTo", ViewState["Value3"].ToString()));
            sqlstr += " and " + ViewState["Name2"].ToString() + " between @DateFrom and @DateTo ";
        }
        else if (ViewState["Value2"] != null && ViewState["Value3"] == null)
        {
            list.Add(new SqlParameter("@DateFrom", ViewState["Value2"].ToString().ToString()));
            sqlstr += " and " + ViewState["Name2"].ToString() + " >= @DateFrom ";
        }
        else if (ViewState["Value2"] == null && ViewState["Value3"] != null)
        {
            list.Add(new SqlParameter("@DateFrom", ViewState["Value3"].ToString().ToString()));
            sqlstr += " and " + ViewState["Name2"].ToString() + " <= @DateFrom ";
        }
        if (ViewState["Shipper"] != null)
        {
            list.Add(new SqlParameter("@ShipperID", ViewState["Shipper"].ToString()));
            sqlstr += " and shipper.PKID=@ShipperID";
        }
        if (ViewState["Vendor"] != null)
        {
            list.Add(new SqlParameter("@VendorID", ViewState["Vendor"].ToString()));
            sqlstr += " and vendor.PKID=@VendorID";
        }

        if (ViewState["Whouse"] != null)
        {
            list.Add(new SqlParameter("@WhouseID", ViewState["Whouse"].ToString()));
            sqlstr += " and whse.PKID=@WhouseID";
        }
        parms = list.ToArray();
    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (txtDateFrom.Text != string.Empty && txtDateTo.Text != string.Empty)
        {
            ViewState["Value2"] = txtDateFrom.Text.Trim() + " 00:00:00";
            ViewState["Name2"] = ddlDateString.SelectedValue;
            ViewState["Value3"] = txtDateTo.Text.Trim() + " 23:59:59";
        }
        else if (txtDateFrom.Text != string.Empty && txtDateTo.Text == string.Empty)
        {
            ViewState["Value2"] = txtDateFrom.Text.Trim() + " 00:00:00";
            ViewState["Name2"] = ddlDateString.SelectedValue;
            ViewState["Value3"] = null;
        }
        else if (txtDateFrom.Text == string.Empty && txtDateTo.Text != string.Empty)
        {
            ViewState["Value2"] = null;
            ViewState["Name2"] = ddlDateString.SelectedValue;
            ViewState["Value3"] = txtDateTo.Text.Trim() + " 00:00:00";
        }
        else
        {
            ViewState["Value2"] = null;
            ViewState["Name2"] = null;
            ViewState["Value3"] = null;
        }

        if (ddlShipper.SelectedValue != "0")
        {
            ViewState["Shipper"] = ddlShipper.SelectedValue;
        }
        else
        {
            ViewState["Shipper"] = null;
        }

        if (ddlVendor.SelectedValue != "0")
        {
            ViewState["Vendor"] = ddlVendor.SelectedValue;
        }
        else
        {
            ViewState["Vendor"] = null;
        }

        if (ddlWhouse.SelectedValue != "0")
        {
            ViewState["Whouse"] = ddlWhouse.SelectedValue;
        }
        else
        {
            ViewState["Whouse"] = null;
        }

        PreviewReport();
    }
}
